<script setup lang="tsx">
import { useRouter } from 'vue-router'
import { ref, unref } from 'vue'
import { useTable } from '@/hooks/web/useTable'
import { getListApi } from '@/api/market/index'
import { useI18n } from '@/hooks/web/useI18n'
import { CurdData } from '@/api/inventory/types'
import { ActionButtonsTemplate } from '@/api/global/types'
import useSchema from './components/hooks/useSchema'
import { useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { Search } from '@/components/Search'
import { BaseButton } from '@/components/Button'
import { ContentWrap } from '@/components/ContentWrap'
import { Table } from '@/components/Table'
import DialogGlobalPrompts from './components/DialogGlobalPrompts/index.vue'

const { push } = useRouter()

const searchParams = ref({})
const dialogGlobalPromptRef = ref()

const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}

const multipleSelection = ref<any[]>([])

const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val
}

const handlePromptWords = () => {
  nextTick(() => {
    dialogGlobalPromptRef.value.dialogVisible = true
    dialogGlobalPromptRef.value.getBackShow()
  })
}
const { tableRegister, tableState, tableMethods } = useTable({
  immediate: true,
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    const params = {
      ...unref(searchParams)
    } as Record<string, any>
    if (params.time) {
      params.start_date = `${params.time[0]} 00:00:00`
      params.end_date = `${params.time[1]} 23:59:59`
      delete params.time
    }
    const res = await getListApi({
      pageIndex: unref(currentPage),
      pageSize: unref(pageSize),
      ...params
    })
    return {
      list: res.data.list,
      total: res.data.total
    }
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList } = tableMethods

const { t } = useI18n()

const action = (row: CurdData, type: string) => {
  push(`/market/industryNews/${type}?id=${row.id}`)
}

const actionButtonsTemplate: ActionButtonsTemplate = {
  length: 3,
  template: (data: any) => {
    return (
      <>
        <BaseButton size="small" type="success" onClick={() => action(data.row, 'detail')}>
          {t('action.detail')}
        </BaseButton>
      </>
    )
  }
}
const { crudSchemas } = useSchema({
  actionButtonsTemplate
})
const { allSchemas } = useCrudSchemas(crudSchemas)
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />

    <div class="mb-10px">
      <!--      <BaseButton-->
      <!--          type="primary"-->
      <!--          @click="handlePromptWords"-->
      <!--      >-->
      <!--        {{ t('market.industryNews.globalPrompts') }}-->
      <!--      </BaseButton>-->
    </div>

    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
      @selection-change="handleSelectionChange"
    />
    <DialogGlobalPrompts ref="dialogGlobalPromptRef" @refresh="getList" />
  </ContentWrap>
</template>
